package vip.websky.project.user.server;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.websky.project.common.error.CommonsRuntimeException;
import vip.websky.project.common.utils.ObjectConvertUtils;
import vip.websky.project.core.log.KingLog;
import vip.websky.project.module.bo.user.SysPermissionBO;
import vip.websky.project.module.entity.user.SysPermission;
import vip.websky.project.user.dao.PermissionDao;
import vip.websky.project.user.error.UserErrorCode;
import vip.websky.project.user.error.UserErrorMessage;

import java.util.List;

/**
 * 系统权限 服务类
 *
 * @Author YangYong E-mail:yang2048@qq.com
 * @Create 2017/9/24-22:16
 **/
@Slf4j
@Service
public class PermissionServer extends ServiceImpl<PermissionDao, SysPermission> {

    @Autowired
    private PermissionDao permissionDao;

    /**
     * 1.创建
     *
     * @param permissionBO 信息
     * @return SysPermissionBO
     */
    @KingLog
    public SysPermissionBO insertPermission(SysPermissionBO permissionBO) {
        SysPermission permission = ObjectConvertUtils.copyToDest(permissionBO, SysPermission.class);
        Integer result = permissionDao.insert(permission);
        return result > 0 ? ObjectConvertUtils.copyToDest(permission, SysPermissionBO.class) : null;
    }

    /**
     * 2.根据条件查找一个
     *
     * @param permissionBO 唯有条件
     * @return SysPermissionBO
     */
    @KingLog
    public SysPermissionBO selectPermission(SysPermissionBO permissionBO) {
        SysPermission permission = ObjectConvertUtils.copyToDest(permissionBO, SysPermission.class);
        permission = permissionDao.selectById(permission.getId());
        return permission != null ? ObjectConvertUtils.copyToDest(permission, SysPermissionBO.class) : null;
    }

    /**
     * 3.获取列表
     *
     * @param permissionBO 信息
     * @return Page<SysPermissionBO>
     */
    @KingLog
    public List<SysPermissionBO> selectPermissionList(SysPermissionBO permissionBO) {
        EntityWrapper<SysPermission> ew = new EntityWrapper<>();
        //TODO 查询条件
        /*ew.orderBy("ID", false);*/
        List<SysPermission> permissionList = permissionDao.selectList(ew);
        //组合数据并封装成Bo
        List<SysPermissionBO> permissionBOList = Lists.newArrayList();
        for (SysPermission permission : permissionList) {
            SysPermissionBO roleBOTemp = new SysPermissionBO();
            ObjectConvertUtils.convert(permission, roleBOTemp);
            permissionBOList.add(roleBOTemp);
        }
        return permissionBOList;
    }

    /**
     * 4.修改信息
     *
     * @param permissionBO 信息
     * @return SysPermissionBO
     */
    @KingLog
    public SysPermissionBO updatePermission(SysPermissionBO permissionBO) {
        SysPermission permission = ObjectConvertUtils.copyToDest(permissionBO, SysPermission.class);
        Integer result = permissionDao.updateById(permission);
        return result > 0 ? ObjectConvertUtils.copyToDest(permission, SysPermissionBO.class) : null;
    }

    /**
     * 5.删除
     *
     * @param permissionBO id
     * @return boolean
     */
    @KingLog
    public boolean delPermission(SysPermissionBO permissionBO) {
        SysPermission permission = ObjectConvertUtils.copyToDest(permissionBO, SysPermission.class);
        Integer result = permissionDao.deleteById(permission.getId());
        return result > 0;
    }
}
